package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.util.OSSUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @Author longbosheng
 * @Date 2022/6/15 16:21
 * @PackageName:com.itheima.util
 * @ClassName: UploadPicture
 * @Description: TODO
 * @Version 1.0
 */
@RestController
@RequestMapping("/common")
public class UploadPicture {
    /**
     * OSS文件上传
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public R<String> upload(MultipartFile file) throws IOException {

        //获取文件的原生名字
        String originalFilename = file.getOriginalFilename();
        //获取后缀
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //防止图片名称重复，使用UUID生成名字
        String fileName = UUID.randomUUID().toString()+suffix;

        InputStream inputStream = file.getInputStream();
        String upload = OSSUtils.upload(inputStream, fileName);
        return R.success(upload);
    }


    /**
     * OSS文件下载(移动端)
     * @param name
     * @param response
     */
//    @GetMapping("/download")
//    public void download(String name, HttpServletResponse response) throws Exception {
//        OSSUtils.download(name,response);
//    }


}
